package com.java2007.hotel.controller;

import com.java2007.hotel.pojo.FoodType;
import com.java2007.hotel.service.FoodTypeService;
import com.java2007.hotel.service.impl.FoodTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author ghy
 * @version 1.0
 * @date 2021-01-21
 */
//@WebServlet("/foodtype/search")
public class FoodTypeSearchServlet extends HttpServlet {

    private FoodTypeService foodTypeService = new FoodTypeServiceImpl();


    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {

            String keyword = request.getParameter("keyword");

            //List<FoodType> types = foodTypeService.findAll();
            List<FoodType> types = foodTypeService.findByTypeName(keyword);
            request.setAttribute("types", types);
            request.setAttribute("keyword", keyword);
            request.getRequestDispatcher("/backend/detail/foodtype/food-type-list.jsp").forward(request, response);
        } catch (SQLException e) {
            e.printStackTrace();
            request.setAttribute("errorMsg", "服务器正忙，请稍后重试！");
            request.getRequestDispatcher("../../backend/index.jsp").forward(request, response);
            //response.sendRedirect(request.getContextPath() + "/backend/index.jsp");
        }

    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
